import 'package:enterprise_v6_app/app/components/toast_widget.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/get_dict_all_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/common/risk_tree_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/get_tb_base_risk_events_by_id_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/risk_control_page/risk_security_identification_page/risk_security_identification_controller.dart';
import 'package:get/get.dart';

///风险管控-》安全风险辨识-》点击+新增界面 事件新增
class RiskOnClickAddEventPageController extends GetxController {
  /// 选中的 隐患排查任务管理_管控方式
  GetDictAllEntity? dictEntity;

  ///风险事件名称
  String eventName = '';

  ///初始后果
  String initialConsequences = '';

  ///初始可能性
  String initialPossibility = '';

  ///初始风险度
  String initialRiskLevel = '';

  ///初始风险等级 id
  String currencyDangerLevelId = '';

  ///初始风险等级 name
  String currencyDangerLevelName = '';

  //默认新增
  bool addAndUpdate = true;

  ///获取相应数据
  GetTbBaseRiskEventsByIdEntity? data;

  /// 选中的节点
  RiskTreeEntity selectedEntity = RiskTreeEntity();

  @override
  void onInit() {
    super.onInit();
    selectedEntity = Get.arguments['riskUnitId'] as RiskTreeEntity;
    if (Get.arguments['editId'] != null) {
      addAndUpdate = false;
      getRiskUnitById();
    }
  }
  void isBool() {
    update();
  }

  ///新增或更新风险分析事件
  void addEventObject() async {
    if (eventName.isEmpty) {
      AppToast.showWarning('请输入风险事件名称');
      return;
    }
    if (initialConsequences.isEmpty) {
      AppToast.showWarning('请输入初始后果');
      return;
    }
    if (initialPossibility.isEmpty) {
      AppToast.showWarning('请输入初始可能性');
      return;
    }
    if (initialRiskLevel.isEmpty) {
      AppToast.showWarning('请输入初始风险度');
      return;
    }
    if (currencyDangerLevelName.isEmpty) {
      AppToast.showWarning('请选择初始风险等级');
      return;
    }
    // 新增或更新风险分析对象
    final res = await HttpUtil().generatePostRequest(addAndUpdate ? Interface.insertTbBaseRiskEvents : Interface.updateTbBaseRiskEvents, data: {
      'id': addAndUpdate ? null : Get.arguments['editId'], //风险分析事件id，如果为新增，则不需要该参数
      'initialRiskConsequences': initialConsequences, //	初始风险后果
      'initialRiskDegree': initialRiskLevel, //初始风险度
      'initialRiskLevel': currencyDangerLevelId, //初始风险等级：1_重大2_较大3_一般4_低
      'initialRiskPossibility': initialPossibility, //初始风险可能性
      'riskEventName': eventName, //风险事件名称
      // 'riskUnitId': Get.arguments['riskUnitId'], //	风险分析单元uuid
      'riskUnitId': selectedEntity.id, //	风险分析单元uuid
    }).execute(isShowLoading: true);
    res.onSuccess((result) {
      Get.back(closeOverlays: true);
      /// 当前打开的snackbar/dialog/bottomsheet 和当前页面
      // 刷新列表
      final RiskSecurityIdentificationController riskController = Get.find();
      riskController.globalKey.currentState?.onRefresh();
    });
    res.onError((result) {
      AppToast.showError('新增失败~');
    });
  }

  /// 请求参数
  Map<String, dynamic> queryParameters = {};

  getRiskUnitById() async {
    queryParameters['id'] = Get.arguments['editId'];
    final res = await HttpUtil()
        .generateGetRequest<GetTbBaseRiskEventsByIdEntity>(
          Interface.getTbBaseRiskEventsById,
          queryParameters: queryParameters,
        )
        .execute();
    res.onSuccess((result) {
      if (result.data != null) {
        data = result.data!;
      }
    });

    eventName = data?.riskEventName ?? '';
    initialConsequences = '${data?.initialRiskConsequences}';
    initialPossibility = '${data?.initialRiskPossibility}';
    initialRiskLevel = '${data?.initialRiskDegree}';
    currencyDangerLevelId = '${data?.initialRiskLevel}';
    //	初始风险等级：1_重大 2_较大 3_一般 4_低
    if (data?.initialRiskLevel == '1') {
      currencyDangerLevelName = '重大风险';
    } else if (data?.initialRiskLevel == '2') {
      currencyDangerLevelName = '较大风险';
    } else if (data?.initialRiskLevel == '3') {
      currencyDangerLevelName = '一般风险';
    } else if (data?.initialRiskLevel == '4') {
      currencyDangerLevelName = '低风险';
    }
    update();
  }

  ///初始风险等级：1_重大 2_较大 3_一般 4_低
  String backRiskLevelName(double riskDegree) {
    if (1 <= riskDegree && riskDegree < 9) {
      return '低风险'; //4
    }
    if (9 <= riskDegree && riskDegree < 15) {
      return '一般风险'; //3
    }
    if (15 <= riskDegree && riskDegree < 20) {
      return '较大风险'; //2
    }
    if (20 <= riskDegree && riskDegree <= 25) {
      return '重大风险'; //1
    }
    return ''; //空
  }

  ///初始风险等级：1_重大 2_较大 3_一般 4_低
  String backRiskLevelId(double riskDegree) {
    if (1 <= riskDegree && riskDegree < 9) {
      return '4';
    }
    if (9 <= riskDegree && riskDegree < 15) {
      return '3';
    }
    if (15 <= riskDegree && riskDegree < 20) {
      return '2';
    }
    if (20 <= riskDegree && riskDegree <= 25) {
      return '1';
    }
    return '';
  }
}
